

let tableIns;
let tree;
layui.use(['table', 'form','layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table , form = layui.form;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;

    findMoney();

    tableIns = table.render({
        elem: '#rechargeTable'
        , url: '/platform/recharge/list/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , title: '充值记录'
        	
        , cols: [[
            {field: 'id', title: 'ID', hide:true}
            , {field: 'companyName',title: '商户名称'}
            , {field: 'amount',title: '充值金额',templet: function(d){return formatMoney(d.amount);}}
            , {field: 'serviceCenter', title: '服务商'}
            , {field: 'transferVoucher', title: '转账凭证' ,templet:'#imgtmp'}
            , {field: 'createTime', title: '申请时间'}
            , {field: 'updateTime', title: '操作时间'}
            , {field: 'comments', title: '备注'}
            , {field: 'status', title: '状态', toolbar:"#staBar"}
        ]]
        ,id:'testReload'
        ,defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
        , limit : 10
        , limits: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    });

    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload'),
                _data = {};
            if(demoReload.val() != ""){
                _data = {companyName: demoReload.val()};
            }
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , done: function (res, curr, count) {
                    //完成后重置where，解决下一次请求携带旧数据
                    this.where = {};
                }
                ,where: _data
            }, 'data');
            findMoney();
        }
    };
    $('.demoTable .layui-btn.search').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    form.on('select(editSta)',function (obj) {
    	layer.confirm('确定要变更状态吗？', function (index) {
        var _val = obj.value,
            _status = _val.split('|')[0],
            _id = _val.split('|')[1];
        $.post("/platform/recharge/update", {status:_status,id:_id}, function (res) {
            var data = JSON.parse(res);
            console.log("flag=="+res);
            console.log("data=="+data);
            if(data.flag){
                layer.msg("修改成功", {icon: 1, time: 2000}, function(){
                    $('#demoReload').val('');
                    $('.demoTable .layui-btn.search').click();
                });
            } else {
                layer.msg("修改失败!", {icon: 5});
            }
        });
    });
    }); 
    
    form.on('submit(exportAll)', function(){
    	location.href= '/platform/recharge/export?sellerName='+$("#demoReload").val();
		  //alert("导出");
	  });

});

function show_img(t) {
    var t = $(t).find("img");
    //页面层
    layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['80%', '80%'], //宽高
        shadeClose: true, //开启遮罩关闭
        end: function (index, layero) {
            return false;
        },
        content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '" /></div>'
    });
}
function postStaFun(obj) {
    var _status = $(obj).val();
    var _id = $(obj).data('id');
    $.post("/platform/recharge/update", {status:_status,id:_id}, function (data) {
        if(data.flag){
            layer.msg("修改成功", {icon: 1, time: 2000}, function(){
                $('#demoReload').val('');
                $('.demoTable .layui-btn.search').click();
            });
        } else {
            layer.msg("修改失败!", {icon: 5});
        }
    });
    return;
}
function formatMoney(str) {
    if (!str) str = '0.00';
    let ret = Math.round(parseFloat(str) * 100) / 100;
    let decimal = ret.toString().split('.');
    if (decimal.length === 1) {
        return ret.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            + '.00';
    };
    if (decimal.length > 1) {
        decimal[0] = decimal[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        if (decimal[1].length < 2) {
            decimal[1] += '0';
            return decimal.join(".");
        }
        return decimal.join(".");
    };
    return ret;
}


function findMoney(){
    $.post('/platform/recharge/queryByMoney?sellerName='+ $("#demoReload").val() , {}, function (data) {
    	$("#sum_money2").html("审核总金额："+formatMoney(data.data[0].examineing));
                $("#sum_money1").html("充值总金额："+formatMoney(data.data[0].examineed));
                $("#sum_money3").html("取消总金额："+formatMoney(data.data[0].examineno));
    })
}